import { request, RequestOptions } from '@umijs/max';
import { message } from 'antd';
import { SortOrder } from 'antd/lib/table/interface';

// 封装 get 方法
// proTable 参数中自带 current&pageSize
export function get<T = any>(
  url: string,
  params: any = {},
  sqlAppend: string = '',
  sort: Record<string, SortOrder> = {},
  filter: Record<string, (string | number)[] | null> = {},
  opts: RequestOptions = {},
): Promise<T> {
  return qry<T>('GET', url, params, sqlAppend, sort, filter, opts);
}

// 封装 POST 方式 get 数据
// proTable 参数中自带 current&pageSize
export function postGet<T = any>(
  url: string,
  params: any = {},
  sqlAppend: string = '',
  sort: Record<string, SortOrder> = {},
  filter: Record<string, (string | number)[] | null> = {},
  opts: RequestOptions = {},
): Promise<T> {
  // 调用 request 方法，并预设 method 为 'get'
  return qry<T>('POST', url, params, sqlAppend, sort, filter, opts);
}

function qry<T = any>(
  methord: string,
  url: string,
  params: any = {},
  sqlAppend: string = '',
  sort: Record<string, SortOrder> = {},
  filter: Record<string, (string | number)[] | null> = {},
  opts: RequestOptions = {},
): Promise<T> {
  if (filter) {
    for (const key in filter) {
      if (filter.hasOwnProperty(key)) {
        if (filter[key]) {
          params[key] = filter[key];
        }
      }
    }
  }

  if (sort) {
    for (const key in sort) {
      if (sort.hasOwnProperty(key)) {
        params['sqlAppend'] =
          'order by ' + key + ' ' + (sort[key] === 'ascend' ? 'asc' : 'desc');
      }
    }
  }
  if (sqlAppend !== '') {
    if (params['sqlAppend']) {
      params['sqlAppend'] += ' ' + sqlAppend;
    } else {
      params['sqlAppend'] = sqlAppend;
    }
  }

  // 调用 request 方法，并预设 method 为 'get'
  return request<T>(url, {
    method: methord,
    params: methord === 'GET' ? params : undefined,
    data: methord === 'POST' ? params : undefined,
    ...opts,
  });
}

// 封装 post 方法
export function post<T = any>(url: string, data: any, opts: RequestOptions = {}): Promise<T> {
  // 调用 request 方法，并预设 method 为 'post'
  // 如果需要设置 content-type 或其他 headers，可以在 options 中指定
  return request(url, {
    ...opts,
    method: 'POST',
    data,
  });
}

// 封装 put 方法
export function put<T = any>(
  url: string,
  where: any,
  set: any,
  opts: RequestOptions = {},
): Promise<T> {
  // 调用 request 方法，并预设 method 为 'put'
  // 如果需要设置 content-type 或其他 headers，可以在 options 中指定
  for (const key in where) {
    if (!Object.hasOwn(where, key)) {
      delete where[key];
    }
  }
  for (const key in set) {
    if (!Object.hasOwn(set, key)) {
      delete set[key];
    }
  }
  if (Object.keys(where).length === 0) {
    message.error('where 不能为空');
    return Promise.reject('where is empty');
  }
  if (Object.keys(set).length === 0) {
    message.error('set 不能为空');
    return Promise.reject('set is empty');
  }

  return request(url, {
    ...opts,
    method: 'PUT',
    data: {
      where,
      set,
    },
  });
}

// 封装 del 方法
export function del<T = any>(url: string, where: any, opts: RequestOptions = {}): Promise<T> {
  // 调用 request 方法，并预设 method 为 'put'
  // 如果需要设置 content-type 或其他 headers，可以在 options 中指定
  return request(url, {
    ...opts,
    method: 'DELETE',
    data: where,
  });
}